/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.carpark.rest;

import cz.muni.fi.pa165.carpark.EmployeePosition;
import cz.muni.fi.pa165.carpark.TO.VehicleTO;
import cz.muni.fi.pa165.carpark.VehicleStatus;
import cz.muni.fi.pa165.carpark.servicies.VehicleService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 * @author Ado
 */
@Controller
@RequestMapping("/vehicle")
public class VehicleRestService {
    
    @Autowired
    private VehicleService service;
    
    
    @RequestMapping(value="/{id}", method = RequestMethod.GET)
    public @ResponseBody VehicleTO get(@PathVariable("id") Long id) {
        return service.retrieve(id);
    }
    
    @RequestMapping(value="/", method = RequestMethod.POST)
    public void create(@RequestBody VehicleTO veh, HttpServletRequest req, HttpServletResponse resp) {
        service.create(veh);
        resp.setStatus(HttpStatus.CREATED.value());
        resp.setHeader("Location", req.getContextPath() + "/rest/vehicle/" + veh.getId());
    }
    
    @RequestMapping(value="/{id}", method = RequestMethod.PUT)
    public void update(@PathVariable("id") Long id, @RequestBody VehicleTO veh, HttpServletResponse resp) {
        service.update(veh);
        resp.setStatus(HttpStatus.OK.value());
    }
    
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void delete(@PathVariable("id") Long id) {
        VehicleTO veh = service.retrieve(id);
        service.delete(veh);
    }
    
    
    @RequestMapping(value = "/findall", method = RequestMethod.GET)
    public @ResponseBody List<VehicleTO> find() {
        return service.retrieveAllVehicles();
    }
    
    
    @RequestMapping(value = "/findbycategory/{category}", method = RequestMethod.GET)
    public @ResponseBody List<VehicleTO> findByCategory(@PathVariable("category") EmployeePosition position) {
        return service.retrieveAllVehiclesForPosition(position);
    }
    
    @RequestMapping(value = "/findbystatus/{status}", method = RequestMethod.GET)
    public @ResponseBody List<VehicleTO> findByStatus(@PathVariable("status") VehicleStatus status) {
        return service.getVehiclesByStatus(status);
    }
}
